import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:hprt_cpcl/hprt_cpcl.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: buildContent(context)
        ),
      ),
    );
  }

  Widget buildContent(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // TextButton(onPressed: () async {
        //   int res = (await HprtCpcl.portOpenBT('FC:58:FA:2D:99:6A'))!;
        //   print(res);
        // }, child: const Text('connect')),
        


        TextButton(onPressed: () async {


          // 读取资源文件
          ByteData data = await rootBundle.load("assets/images/2222.jpeg");
          // 转byte
          List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);


          await HprtCpcl.portClose();
          await HprtCpcl.portOpenBT('FC:58:FA:2D:99:6A');
          // await HprtCpcl.setSelf();

          String content = '200127^6d9319126d7344fdb76b7a010ce7473d^89570f738c5847f5ab1caeb1c350fc1f^2000000002^H-2-4^^4^120^1^^1^100^1^10^1^5^1^^7^^1^^1^^3^送货 签回单^37dff08ad45e4d0b848b0247f03c4f8f^2021-11-08^';

          // await HprtCpcl.printAreaSize(
          //   offset: '0',
          //   horizontal: '200',
          //   vertical: '200',
          //   height: '1400',
          //   qty: '1'
          // );
          // await HprtCpcl.printQR(command:'BARCODE', x:'0',y:'0',m:'2',u:'6',data: '离离原上草，一岁一枯荣helloworld');

          await HprtCpcl.printBitmap(bytes: bytes, x: 0, y: 0, width: 1000, height: 600, rotate: 90);


          // await HprtCpcl.printQrCodeImage(
          //   data: content,
          //   height: 350,
          //   width: 350,
          //   x: 0,
          //   y: 0,
          // );

          await HprtCpcl.print();
          // await HprtCpcl.portClose();

        }, child: const Text('setSelf2')), 
      ],
    );
  }

}
